package oceanlife.pathfinding;

import java.util.ArrayList;
import java.util.HashMap;

import oceanlife.OLSet;

public class PathTriangle{
	private Triangle tri;
	private HashMap<Pnt,Pnt> oppositePnts = new HashMap<Pnt,Pnt>();
	private Pnt middle;
	
	
	public PathTriangle(Triangle tri, HashMap<Pnt, SimpleObstacle> obsRepos){
		this.tri = tri;
		Pnt[] pnts = tri.toArray(new Pnt[0]);
		Pnt pnt1 = pnts[0];
		Pnt pnt2 = pnts[1];
		middle = new Line2(pnt1,tri.getOppositeEdge(pnt1).getMiddlePnt()).intersectionPoint(
				 new Line2(pnt2,tri.getOppositeEdge(pnt2).getMiddlePnt()));
		for(Pnt pnt : pnts){
			Line2 edge = tri.getOppositeEdge(pnt);
			Pnt startOb = null;
			Pnt endOb = null;
			SimpleObstacle so = obsRepos.get(edge.getStart());
			for(Line2 obLine : so.getObstacleEdges()){
				Pnt inter = obLine.intersectionPoint(edge);
				if(inter != null){
					startOb = inter;
					break;
				}
			}
			for(Line2 obLine : obsRepos.get(edge.getEnd()).getObstacleEdges()){
				Pnt inter = obLine.intersectionPoint(edge);
				if(inter != null){
					endOb = inter;
					break;
				}
			}
			oppositePnts.put(pnt,new Line2(startOb,endOb).getMiddlePnt());
		}
		
		//check if obstacle crosses opposite edge
		SimpleObstacle borderCrossing = simpleObstacleCrossingEdge(obsRepos);
		if(borderCrossing != null){
			
		} else {
			//check if obstacle crosses paths in triangle
			SimpleObstacle pathCrossing = simpleObstacleCrossingInternalPath(obsRepos);
			if(pathCrossing != null){
				Line2 closestLine = null;
				for(Pnt corner : pathCrossing.getObstacleCorners()){
	//				Pnt potPnt = tri.getOppositeEdge(pathCrossing).closestPoint(corner);
		//			if (closestLine == null){
			//			closestLine = new Line2(potPnt,corner);
				//	} else if(new Line2(potPnt,corner).length() < closestLine.length()){
					//	closestLine = new Line2(potPnt,corner);
					//}
				}
				//middle = closestLine.getMiddlePnt();
			}
		}
	}
	
	public SimpleObstacle simpleObstacleCrossingInternalPath(HashMap<Pnt, SimpleObstacle> obsRepos){
		//for each obstacle
		for(Pnt pnt : tri){
			SimpleObstacle so = obsRepos.get(pnt);
			int pathLinesCrossed = 0;
			//for each pathline
			for(Pnt pathPnt : tri){
				Line2 pathLine = new Line2(middle,oppositePnts.get(pathPnt));
				//for each obstacle edge
				for(Line2 obstacleEdge : so.getObstacleEdges()){
					if(obstacleEdge.intersectionPoint(pathLine) != null){
						pathLinesCrossed++;
					}
				}
			}
			if(pathLinesCrossed > 0){
				return so;
			}
		}
		return null;
	}
	
	public SimpleObstacle simpleObstacleCrossingEdge(HashMap<Pnt, SimpleObstacle> obsRepos){
		for(Pnt pnt : tri){
			SimpleObstacle so = obsRepos.get(pnt);
			for(Pnt obstaclePoint : so.getObstacleCorners()){
				if(new Line2(obstaclePoint,pnt).intersectionPoint(tri.getOppositeEdge(pnt)) != null){
					return so;
				}
			}
		}
		return null;
	}
	
	
	public ArrayList<Line2> getPathLines(){
		ArrayList<Line2> lines = new ArrayList<Line2>();
		for(Pnt pnt : tri){
			lines.add(new Line2(middle,oppositePnts.get(pnt)));
		}
		return lines;
	}
}
